﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using svs.Mobile.BeyondMedia.Objects;
using svs.Mobile.BeyondMedia.UiCore;

namespace svs.Mobile.BeyondMedia.Forms.Settings
{
	public partial class SettingsPageHolderForm : Form
	{
		#region SettingsPages enum

		public enum SettingsPages
		{
			General = 0,
			Player,
			FeedContent
		}

		#endregion

		public ISettingsPage _CurrentPage = null;
		readonly InputPanel _InputPanel;

		public SettingsPageHolderForm(SettingsPages page)
		{
			KeyPreview = true;

			switch (page)
			{
				case SettingsPages.General:
					_CurrentPage = new GeneralSettings();
					break;

				case SettingsPages.Player:
					_CurrentPage = new PlayerSettings();
					break;

				case SettingsPages.FeedContent:
					_CurrentPage = new FeedContentSettings();
					break;
			}

			Control currentPage = _CurrentPage as Control;
			currentPage.Location = new Point(0, 0);
			currentPage.TabIndex = 1;
			currentPage.Visible = true;

			InitializeComponent();

			if (PlatformDetection.IsSmartphone())
			{
				LinkLabel l1 = new LinkLabel();
				l1.Size = new Size(1, 1);
				l1.Location = new Point(0, 0);
				l1.TabIndex = 0;
				Controls.Add(l1);
			}

			Controls.Add(currentPage);

			if(PlatformDetection.IsSmartphone())
			{
				LinkLabel l2 = new LinkLabel();
				l2.Size = new Size(1, 1);
				l2.Location = new Point(0, currentPage.Bottom);
				l2.TabIndex = 2;
				Controls.Add(l2);
			}

			if (PlatformDetection.IsPocketPC())
			{
				_InputPanel = new InputPanel();
				_InputPanel.EnabledChanged += OnInputPanelChanged;
			}
		}

		private void OnInputPanelChanged(object sender, EventArgs e)
		{
			var currentPage = _CurrentPage as Control;

			if(currentPage == null)
				return;

			if (_InputPanel.Enabled)
			{
				currentPage.Height += _InputPanel.Bounds.Height;
				Helpers.EnsureSIPDoesNotOverlapActiveControl(this, _InputPanel.VisibleDesktop.Height);
			}
			else
				currentPage.Height -= _InputPanel.Bounds.Height;
		}

		public bool ShouldReloadRepositoryOnDone
		{
			get { return _CurrentPage.ShouldReloadRepositoryOnDone; }
		}

		private void Cancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		private void Done_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
		}

		private void OnClosing(object sender, CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				e.Cancel = _CurrentPage.CheckAndSaveSettings() == false;
			}
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize(e);
			Control currentPage = _CurrentPage as Control;
			currentPage.Size = new Size(ClientSize.Width, currentPage.Height);
		}

		private void OnLoad(object sender, EventArgs e)
		{
			_CurrentPage.LoadPageSettings();
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
			switch (e.KeyData)
			{
				case Keys.Up:
				case Keys.Down:
					e.Handled = true;
					SelectNextControl(Helpers.GetFocusedControl(this), e.KeyData == Keys.Down, true, true, true);
					break;

				default:
					break;
			}
		}
	}
}